package annotationDemo.demo2.decription;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

//import com.sun.org.apache.bcel.internal.classfile.InnerClass;

/**
 * Created by dllo on 17/8/21.
 */

/**
 * 注解的反射,即为注解的灵魂
 */
public class ToDo {
    public static void main(String[] args) {
        try {
            // 通过反射去解析注解的属性
            Class<?> c =ActiveChird.class;
            ActiveChird activeChird = new ActiveChird("白色",555);
          
            showBeanData(activeChird);
        
//            Class.forName("decription.ActiveChird");
            // 判断在类上是否使用了自定的注解
            boolean isExist = c.isAnnotationPresent(Decription.class);
            if (isExist){
                // 从类上获得自定义的注解
                Decription d = (Decription) c.getAnnotation(Decription.class);
                System.out.println(d.decription() + d.id());
            }
            // 得到该类及父类中的所有方法
            Method[] ms = c.getMethods();
            // 遍历所有方法
            for (Method m:ms){
                // 判断在方法上是否使用了自定的注解
                boolean isExistMothed = m.isAnnotationPresent(Decription.class);
                if (isExistMothed){
                    // 从方法上获得自定义的注解
                    Decription d = m.getAnnotation(Decription.class);
                    System.out.println(d.decription());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void showBeanData(ActiveChird activeChird) {
    	  Class<? extends ActiveChird> class1 = activeChird.getClass();
          Field[] declaredFields = class1.getDeclaredFields();
          for (int i = 0; i < declaredFields.length; i++) {
			Field field = declaredFields[i];
			Decription annotation = field.getAnnotation(Decription.class);
			if (annotation!=null) {
				field.setAccessible(true);
				try {
					System.out.println(annotation.decription()+"--"+field.getName()+"--"+field.get(activeChird));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
//				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
				}
				
			}
        	  
		}
		
	}
	public void pring(){
        System.out.println("嘿嘿嘿额");
    }
}
